package com.generator.aboutUser.log.controller;

import com.generator.aboutUser.log.pojo.Journal;
import com.generator.aboutUser.log.service.impl.LogService;
import com.generator.aboutUser.user.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/01
 * 星期六 21:26
 */
@Controller
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogService logService;

    public LogService getLogService() {
        return logService;
    }
    public void setLogService(LogService logService) {
        this.logService = logService;
    }


    /**
     * 根据用户ID查询上一次登录时间
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/getLastLoginTime")
    public Journal getLastLoginTime(HttpSession session){
        Journal lastLoginLog = null;
        Users loginUser = (Users) session.getAttribute("user");
        if (loginUser != null){
            lastLoginLog = logService.getLastLoginTime(loginUser);
        }
        return lastLoginLog;
    }

    /**
     * 添加用户登录日志
     * @return
     */
    @ResponseBody
    @RequestMapping("/addLoginLog")
    public boolean addLoginLog(HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        boolean b = false;
        if (user != null){
            b = logService.addLoginLog(user);
        }
        return b;
    }

}
